﻿using Abp.Application.Services.Dto;
using AppFramework.DinosaursParkMes.CodingRuleModel;
using AppFramework.DinosaursParkMes.DomainService;
using AppFramework.DinosaursParkMes.DomainService.CodingRule;
using AppFramework.DinosaursParkMes.WorkOrder.CodingPush;
using AppFramework.DinosaursParkMes.WorkOrder.Dto;
using AppFramework.Dto;
using Stripe;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppFramework.DinosaursParkMes.WorkOrder
{
    public class WorkOrderAppService : AppFrameworkAppServiceBase, IWorkOrderAppService
    {
        //依赖注入
        private readonly ICodingruleManager _manager;
        private readonly ICodingPushExcel _codingPushExcel;

        public WorkOrderAppService(ICodingruleManager manager, ICodingPushExcel codingPushExcel)
        {
            _manager = manager;
            _codingPushExcel = codingPushExcel;
        }

        /// <summary>
        /// 查找单个编码规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public CodingruleDTO GetById(int id)
        {
            var entity = _manager.GetById(id);
            var model = ObjectMapper.Map<CodingruleDTO>(entity);
            return model;
        }
        /// <summary>
        /// 编码的生成
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public Task<string> GetCode(string tableName)
        {
            return _manager.GetCode(tableName);
        }

        /// <summary>
        /// 编码规则的新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResponseCodingruleDTO> InsertCodingrule(RequestCodingDTO model)
        {
            var entity = ObjectMapper.Map<Codingrules>(model);
            Codingrules coding = await _manager.InsertCodingrule(entity);

            var m = ObjectMapper.Map<ResponseCodingruleDTO>(coding);
            return m;
        }
        public async Task<PagedResultDto<CodingruleDTO>> GetPageShow(int pageindex, int pagesize)
        {

            var list = await _manager.GetAll();
            var list1 = list.OrderByDescending(x => x.Id).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
            var Items = ObjectMapper.Map<List<CodingruleDTO>>(list1);
            var Count = list.Count;
            return new PagedResultDto<CodingruleDTO>
            {
                TotalCount = Count,
                Items = Items
            };
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<string> PutCodingrule(RequestCodingDTO model)
        {
            var entity = ObjectMapper.Map<Codingrules>(model);
            Codingrules codingrules = await _manager.Update(entity);
            if (codingrules == null)
            {
                return "修改失败";
            }
            else
            {
                return "修改成功";
            }
        }
        public async Task<FileDto> GetFile()
        {
            //获取所有数据
            var data = await _manager.GetAll();
            //把所有的数据通过ExportToFile方法进行赋值
            var list =  _codingPushExcel.ExportToFile(data);
            return list;
        }
        
    }
}
